#region Using

using System;
using System.Web;
using System.Xml;
using System.IO;
using System.Text;
using BlogEngine.Core;

#endregion

namespace BlogEngine.Core.Web.HttpHandlers
{
    /// <summary>
    /// 
    /// </summary>
    public class OpmlHandler : IHttpHandler
    {

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that 
        /// implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        ///
        /// Creates an OPML formatted XML response based on the Published Link items.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> 
        /// object that provides references to the intrinsic server objects 
        /// (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=opml.xml");

            using (XmlTextWriter writer = new XmlTextWriter(context.Response.Output)) 
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;

                //write the start of the document
                writer.WriteStartDocument(true);
				writer.WriteStartElement("opml");
    			writer.WriteAttributeString("version", "2.0");
	    		writer.WriteAttributeString("xmlns", "xsd",null, "http://www.w3.org/2001/XMLSchema");
		    	writer.WriteAttributeString("xmlns", "xsi",null, "http://www.w3.org/2001/XMLSchema-instance");
                
                //write the head of the document
                writer.WriteStartElement("head");
                writer.WriteElementString("title","BlogRoll for " + BlogSettings.Instance.Name);
                writer.WriteElementString("ownerName",BlogSettings.Instance.AuthorName);
                writer.WriteElementString("dateCreated",SyndicationGenerator.ToRfc822DateTime(DateTime.Now));
                writer.WriteElementString("dateModified", SyndicationGenerator.ToRfc822DateTime(DateTime.Now));
                writer.WriteEndElement();

                //write the boday and any links
                writer.WriteStartElement("body");

                foreach (Link link in Link.Items)
                {
                    if (link.IsPublished && link.BlogRoll)
                    {
                        writer.WriteStartElement("outline");
                        writer.WriteAttributeString("text", link.Title);
                        writer.WriteAttributeString("title", link.Title);
                        writer.WriteAttributeString("description", link.Description);
                        writer.WriteAttributeString("xfn", link.XFNTag);
                        writer.WriteAttributeString("xmlUrl", link.Feed);
                        writer.WriteAttributeString("htmlUrl", link.Website);
                        writer.WriteEndElement();    
                    }
                }

                //write the end body tag
                writer.WriteEndElement();

                //write the end opml element
                writer.WriteEndElement();
            }
        }

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"></see> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }
  }
}
